﻿//Copyright by Mirco Töpfer


#region Usings
using System;
using System.Windows.Forms;
using Match.Foundation.Business.Classes;
using Match.ManagementApplication.AbstractClasses;
using Match.ManagementApplication.Classes;
#endregion

namespace Match.ManagementApplication.Forms
{
    public partial class frmStartClass : MatchForm
    {

        #region Private Members
        private StartClass _StartClass;
        private Boolean _IsChanged;
        #endregion

        #region Constructor
        public frmStartClass(FormName FormName, String ShownName, String ShownDescription, StartClass StartClass)
            : base(FormName, ShownName, ShownDescription)
        {
            InitializeComponent();
            if (StartClass == null) _StartClass = new StartClass();
            else _StartClass = StartClass;
            this.Text = ShownName;
            this.Initialize();
        }
        #endregion

        #region FormEvents
        private void numericUpDownStartAge_ValueChanged(object sender, EventArgs e)
        {
            textBoxAgeGroup.Text = (DateTime.Now.Year - Convert.ToInt32(numericUpDownStartAge.Value)).ToString();
            _IsChanged = true;
        }
        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            this.Save();
        }
        private void textBoxName_TextChanged(object sender, EventArgs e)
        {
            _IsChanged = true;
        }
        private void frmStartClass_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            if (_IsChanged)
            {
                DialogResult Result =
                    MessageBox.Show("Möchten Sie die Änderungen speichern?", "Speichern", MessageBoxButtons.YesNoCancel,
                                    MessageBoxIcon.Question);
                if (Result == DialogResult.Yes)
                    Save();
                else if (Result == DialogResult.Cancel)
                    e.Cancel = true;
            }
        }
        private void buttonUndo_Click(object sender, EventArgs e)
        {
            Initialize();
        }
        #endregion

        #region Private Methods
        private void Save()
        {
            _StartClass.Name = textBoxName.Text.Trim();
            _StartClass.StartAge = Convert.ToInt32(numericUpDownStartAge.Value);
            _StartClass.Save();
            _IsChanged = false;
        }
        private void Initialize()
        {
            textBoxName.Text = _StartClass.Name;
            numericUpDownStartAge.Value = _StartClass.StartAge;
            numericUpDownStartAge_ValueChanged(null, null);
            _IsChanged = false;
        }
        #endregion
        
    }
}